package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.common.dtas.BaseServiceImpl;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends BaseServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.检查参数
        if (dto==null)return ErrorParam;
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        //2.根据名称分页查询
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName, dto.getName());
        }
        IPage page1 = page(page, wrapper);
        //3.封装返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page1.getTotal());
        responseResult.setData(page1.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult inster(AdChannel adChannel) {
        if (adChannel==null)return ErrorParam;
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return Success;
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        if (adChannel==null||adChannel.getId()==null)return ErrorParam;
        updateById(adChannel);
        return Success;
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id==null)return ErrorParam;
        AdChannel byId = getById(id);
        if (byId==null)return ErrorParam;
        if (byId.getStatus())return ErrorPARAM_INV;
        int a=1/0;
        removeById(id);
        return Success;
    }
}
